﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Yeetop.PayHub.ChinaBank
{
    /// <summary>
    /// 网银在线支付网关
    /// Copyright © Yeetop Inc.(www.yeetop.com)  All rights reserved. 
    /// </summary>
    /// <history>
    /// [JianTao] 2010-01-01 Created 
    /// </history>
    public class ChinaBankPayment : BasePayment
    {
        public ChinaBankPayment()
            : base()
        {
        }

        private ChinaBankTradeInfo Trade;

        public override void PaySend(MerchantInfo merchant, ITradeInfo trade)
        {
            Trade = (ChinaBankTradeInfo)trade;
            Merchant = merchant;

            StringBuilder builder = new StringBuilder();
            builder.Append(CreateFormField("v_mid", Merchant.UID));
            builder.Append(CreateFormField("remark1", Trade.remark1));
            builder.Append(CreateFormField("remark2", Trade.remark2));
            builder.Append(CreateFormField("v_amount", Trade.v_amount.ToString()));
            builder.Append(CreateFormField("v_md5info", SendSign()));
            builder.Append(CreateFormField("v_moneytype", Trade.v_moneytype));
            builder.Append(CreateFormField("v_oid", Trade.v_oid));
            builder.Append(CreateFormField("v_orderaddr", Trade.v_orderaddr));
            builder.Append(CreateFormField("v_orderemail", Trade.v_orderemail));
            builder.Append(CreateFormField("v_ordermobile", Trade.v_ordermobile));
            builder.Append(CreateFormField("v_ordername", Trade.v_ordername));
            builder.Append(CreateFormField("v_orderpost", Trade.v_orderpost.ToString()));
            builder.Append(CreateFormField("v_ordertel", Trade.v_ordertel));
            builder.Append(CreateFormField("v_rcvaddr", Trade.v_rcvaddr));
            builder.Append(CreateFormField("v_rcvemail", Trade.v_rcvemail));
            builder.Append(CreateFormField("v_rcvmobile", Trade.v_rcvmobile.ToString()));
            builder.Append(CreateFormField("v_rcvname", Trade.v_rcvname));
            builder.Append(CreateFormField("v_v_rcvpostmid", Trade.v_rcvpost.ToString()));
            builder.Append(CreateFormField("v_rcvtel", Trade.v_rcvtel));
            builder.Append(CreateFormField("v_url", Trade.v_url));

            //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

            SubmitPayForm(CreateForm(builder.ToString(), Merchant.Gateway));
        }

        protected override PayStatus CheckReturnData(CheckBillDelegate checkBill)
        {
            // 通知数据中必须包含的Key，如果没有表示数据可能非法
            string[] checkParma = { "v_oid", "v_pstatus", "v_pstring", "v_pmode", "v_md5str", "v_amount", "v_moneytype" };

            if (!ReceiveData.NotifyData.ContainsKeys(checkParma))
            {
                return PayStatus.ServerDataIllegal;
            }

            if (ReceiveData.NotifyData["v_md5str"] == NotifySign())
            {
                if (ReceiveData.NotifyData["v_pstatus"].Equals("20"))
                {
                    BillInfo bill = new BillInfo();
                    bill.MerchantUID = Merchant.UID;
                    bill.OrderId = ReceiveData.NotifyData["v_oid"];
                    bill.OrderAmount = ReceiveData.NotifyData["v_amount"];
                    bill.PayPlatform = PayPlatformType.ChinaBank;

                    return checkBill(bill);
                }
                else
                {
                    return PayStatus.Failure;
                }
            }
            else
            {
                return PayStatus.SignError;
            }
        }

        protected override bool CheckNotifyData(CheckUpdateBillDelegate checkUpdateBill)
        {
            // 通知数据中必须包含的Key，如果没有表示数据可能非法
            string[] checkParma = { "v_oid", "v_pstatus", "v_pstring", "v_pmode", "v_md5str", "v_amount", "v_moneytype" };

            if (!ReceiveData.NotifyData.ContainsKeys(checkParma))
            {
                System.Web.HttpContext.Current.Response.Write("error");

                return false;
            }

            PayStatus payStatus;

            if (ReceiveData.NotifyData["v_md5str"] == NotifySign())
            {
                if (ReceiveData.NotifyData["v_pstatus"].Equals("20"))
                {
                    BillInfo bill = new BillInfo();
                    bill.MerchantUID = ReceiveData.NotifyData["v_mid"];
                    bill.OrderId = ReceiveData.NotifyData["v_oid"];
                    bill.OrderAmount = ReceiveData.NotifyData["v_amount"];
                    bill.PayPlatform = PayPlatformType.ChinaBank;

                    payStatus = checkUpdateBill(bill);                    
                }
                else
                {
                    payStatus = PayStatus.Failure;
                }
            }
            else
            {
                payStatus = PayStatus.SignError;
            }

            if (payStatus == PayStatus.Succeed)
            {
                System.Web.HttpContext.Current.Response.Clear();
                System.Web.HttpContext.Current.Response.Write("ok");
                System.Web.HttpContext.Current.Response.End();

                return true;
            }
            else
            {
                System.Web.HttpContext.Current.Response.Clear();
                System.Web.HttpContext.Current.Response.Write("error");
                System.Web.HttpContext.Current.Response.End();

                return false;
            }
        }

        public override PayPlatformType PayPlatform
        {
            get { return PayPlatformType.ChinaBank; }
        }

        protected override string SendSign()
        {
            string t = string.Concat(Trade.v_amount, Trade.v_moneytype, Trade.v_oid, Merchant.UID, Trade.v_url, Merchant.Key);

            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(t, "md5").ToUpper();
        }

        protected override string NotifySign()
        {
            string t = string.Concat(ReceiveData.NotifyData["v_oid"], ReceiveData.NotifyData["v_pstatus"], ReceiveData.NotifyData["v_amount"], ReceiveData.NotifyData["v_moneytype"], Merchant.Key);

            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(t, "md5").ToUpper();
        }
    }
}
